package com.efruit.poadapters;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.efruit.dao.IProductDAO;
import com.efruit.po.Product;
import com.efruit.productInfoMag.IProductInfo;
import com.efruit.productInfoMag.IProductInfoFetcher;

public class ProductInfoFetcherImpl implements 
		IProductInfoFetcher{
	
	//Spring反转
	IProductDAO productDao = null;
	
	public void setProductDao(IProductDAO dao){
		this.productDao = dao;
	}
	public IProductDAO getProductDao(){
		return productDao;
	}

	@Override
	public boolean remove(int id) {
		System.out.println("Fetcher "+id);
		Product product = productDao.findById(id);
		if(product == null){
			System.out.println("Product with id = "+id +"不存在");
			return false;
		}
		productDao.delete(product);
		return true;
	}
	
	@Override
	public IProductInfo add(String productName,
			String url, float price, int count) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Product product = (Product)ctx.getBean("Product");
		
		product.setName(productName);
		product.setUrl(url);
		product.setPrice(price);
		product.setCount(count);
		System.out.println(productName);
		System.out.println(url);
		System.out.println(price);
		System.out.println(count);
		
		productDao.save(product);
		IProductInfo productInfo = (IProductInfo)ctx.getBean("ProductInfoImpl");//Spring����
		productInfo.setProductName(productName);
		productInfo.setUrl(url);
		productInfo.setPrice(price);
		productInfo.setCount(count);
		
		return productInfo;
	}
	@Override
	public IProductInfo update(int id,
			String productName, String url, float price, int count) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Product product = productDao.findById(id);
//		product.setProductId(id);//?????
		product.setName(productName);
		product.setUrl(url);
		product.setPrice(price);
		product.setCount(count);
		productDao.attachDirty(product);
		
		IProductInfo productInfo = (IProductInfo)ctx.getBean("ProductInfoImpl");//Spring����
		productInfo.setProductId(id);
		productInfo.setProductName(productName);
		productInfo.setUrl(url);
		productInfo.setPrice(price);
		productInfo.setCount(count);
		
		return null;
	}
	@Override
	public List<IProductInfo> getAllProductInfo() {
		
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		List<IProductInfo> result = new ArrayList<IProductInfo>();
		List<Product> list = (List<Product>)productDao.findAll();
		int count = list.size();
		for(int i = 0; i < count; i++){
			Product product = list.get(i);
			IProductInfo iProductInfo = (IProductInfo)ctx.getBean("ProductInfoImpl");
			iProductInfo.setProductId(product.getProductId());
			iProductInfo.setProductName(product.getName());
			iProductInfo.setUrl(product.getUrl());
			iProductInfo.setPrice(product.getPrice());
			iProductInfo.setCount(product.getCount());
			result.add(iProductInfo);
		}
		return result;
	}
	
	@Override
	public IProductInfo getProductInfo(int id) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Product tmp = productDao.findById(id);
		if( tmp != null){
			IProductInfo iProductInfo = (IProductInfo)ctx.getBean("ProductInfoImpl");
			iProductInfo.setProductId(tmp.getProductId());
			iProductInfo.setProductName(tmp.getName());
			iProductInfo.setUrl(tmp.getUrl());
			iProductInfo.setCount(tmp.getCount());
			System.out.println("zhaowei : ProductInfoFetcher price = "+ tmp.getPrice());
			iProductInfo.setPrice(tmp.getPrice());
			return iProductInfo;
		}
		return null;
	}




}
